package logic

import (
	"context"
	"errors"
	"my-zero-tiktok/apps/user/models"
	"my-zero-tiktok/common/xerr"

	"my-zero-tiktok/apps/user/pb/pb"
	"my-zero-tiktok/apps/user/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetUser 获取用户
func (l *GetUserLogic) GetUser(in *pb.IDRequest) (*pb.UserInfo, error) {
	user, err := l.svcCtx.UserModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if errors.Is(err, models.ErrNotFound) {
			return nil, xerr.New(xerr.UserNotExistError)
		}
		return nil, xerr.New(xerr.UserGetError)
	}
	return &pb.UserInfo{Id: user.Id, Name: user.Name}, nil
}
